import Vue from 'vue'
// import store from '@/store'
import Router from 'vue-router'
import login from '@/router/login/login'
import user from '@/router/user/user'
import components from '@/router/components/components'
import rent from '@/router/rent/rent'
import rentHouse from '@/router/rentHouse/rentHouse'
import landlord from '@/router/user/landlord'
import tenant from '@/router/user/tenant'
import brandApartment from '@/router/brandApartment/brandApartment'
import publicRentalHouse from '@/router/publicRentalHouse/publicRentalHouse'
import tencentCredit from '@/router/tencentCredit'
import policyMaterial from '@/router/policyMaterial'
import gzNews from '@/router/news/gzNews'
import secondEntrance from '@/router/secondEntrance'
import houseManage from '@/router/houseManage'

Vue.use(Router)

const routes = [
  {
    // 首页
    path: '/',
    redirect: '/home',
    meta: {
      title: '城壹宜居'
    },
    component: resolve => { require(['@/views/Root.vue'], resolve) },
    // redirect: { path: '/' },
    children: [
      {
        name: 'home',
        path: '/home',
        meta: {
          title: '城壹宜居'
        },
        component: resolve => { require(['@/views/home/index'], resolve) }
      },
      // 服务
      {
        name: 'service',
        path: '/service',
        meta: {
          title: '服务'
        },
        component: resolve => { require(['@/views/service/service.vue'], resolve) }
      },
      // 资讯
      {
        name: 'news',
        path: '/news',
        meta: {
          title: '资讯'
        },
        component: resolve => { require(['@/views/news/news.vue'], resolve) }
      },
      // 我的页面
      {
        name: 'user',
        path: '/user',
        meta: {
          title: '我的',
          loginRequire: true
        },
        component: resolve => { require(['@/views/user/userCenter'], resolve) }
      },
      {
        name: 'houseManage',
        path: '/houseManage',
        redirect: '/houseManage/contract',
        meta: {
          title: '房管',
          loginRequire: true
        },
        component: () => import('@/views/houseManage'),
        children: houseManage
      }
    ]
  },
  // 首页二级入口
  ...secondEntrance,
  // 登陆
  ...login,
  // 个人
  ...user,
  // 组件测试
  ...components,
  // 租房
  ...rent,
  // 发布房源
  ...rentHouse,
  // 房东
  ...landlord,
  // 我是租客
  ...tenant,
  // 机构房源
  ...brandApartment,
  // 公租房
  ...publicRentalHouse,
  // 腾讯证信
  ...tencentCredit,
  ...policyMaterial, // 政策资料
  ...gzNews, // 资料下载
  {
    // 租赁合同
    name: 'contractDoc',
    path: '/contractDoc',
    meta: {
      title: '租赁合同',
      loginRequire: true
    },
    component: resolve => { require(['@/components/contract/index'], resolve) }
  },
  {
    path: '/test/shualian',
    component: resolve => { require(['@/views/test/shualian.vue'], resolve) },
    meta: {
      title: ''
    }
  },
  {
    path: '/test/shualianinfo',
    component: resolve => { require(['@/views/test/shualianInfo.vue'], resolve) },
    meta: {
      title: ''
    }
  },
  {
    // 引导页
    name: 'guidePages',
    path: '/guidePages',
    component: resolve => { require(['@/views/guide/guidePages.vue'], resolve) },
    meta: {
      title: '城壹宜居'
    }
  },
  {
    path: '*',
    component: resolve => { require(['@/views/Root.vue'], resolve) },
    meta: {
      title: ''
    }
  }
]

// scrollBehavior:
// - only available in html5 history mode
// - defaults to no scroll behavior
// - return false to prevent scroll
const scrollBehavior = (to, from, savedPosition) => {
  if (savedPosition) {
    // savedPosition is only available for popstate navigations.
    // return savedPosition
    setTimeout(() => {
      window.scrollTo(savedPosition.x, savedPosition.y)
    }, 200)
  }
}

const router = new Router({
  mode: 'history',
  scrollBehavior,
  routes
})
/* eslint-disable */
router.beforeEach((to, from, next) => {
  // 检测目标路由是否需要用户登录
  Vue.$vux.loading.hide()
  // console.log(to.meta && to.meta.loginRequire, to.path, to.component)
  if (to.name !== 'guidePages' && !window.localStorage.getItem('guide')) {
    next('/guidePages')
  } else if (to.meta && to.meta.loginRequire) {
    let userInfo = window.localStorage.getItem('userInfo')
    userInfo = userInfo ? JSON.parse(userInfo) : {}
    // console.log(userInfo)
    if (userInfo.id == 'undefined' || userInfo.id == '' || typeof(userInfo.id) == 'undefined') {
      window.localStorage.setItem('toRouter', to.path)
      next('/login')
    } else {
      if (to.meta.identityValidate && +userInfo.isValidate !== 1) {
        window.localStorage.setItem('toRouter', to.path)
        next('/certify')
      } else {
        next()
      }
    }
  } else if (to.meta && to.meta.identityValidate && +userInfo.isValidate !== 1) {
    window.localStorage.setItem('toRouter', to.path)
    next('/certify')
  } else {
    next()
  }
})

// 监听浏览器回退事件
const goBack = () => {
  window.addEventListener('popstate', function (e) {
    if (window.location.href.includes('verifyHouse') || window.location.href.includes('verifyHouseCommon')) {
      // return false
      alert('确定退出吗')
    } else {
      return true
    }
    // console.log(window.location, window.location.href)
    // router.push('/user')
  }, false)
  const pushHistory = () => {
    var state = {
      title: 'title',
      url: '#'
    }
    window.history.pushState(state, 'title', '#')
  }
  // pushHistory()
}

goBack()

export default router
